package lsl.leetCode.year2023.test0130;

import java.util.HashMap;

/**
 * @author Zed
 * @description
 * @date 2023/1/30 19:45
 */
public class test242 {
    public static void main(String[] args) {
       String s = "ccat"; String t = "acat";
        System.out.println(isAnagram(s,t));
    }

    public static boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        char[] sCharArray = s.toCharArray();
        char[] tCharArray = t.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(sCharArray[i], map.getOrDefault(sCharArray[i], 0) + 1);
        }

        for (int i = 0; i < t.length(); i++) {
            if (map.get(tCharArray[i]) == null) {
                return false;
            }
            Integer count = map.get(tCharArray[i]);
            if (count - 1 < 0) {
                return false;
            }
            map.put(tCharArray[i], count - 1);
        }
        return true;
    }
}
